package com.liwenfeng.iostream;

import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.util.concurrent.FutureTask;

@Log4j2
public class FileReaderTest {
    public static void main(String[] args) throws FileNotFoundException {

//        String fileName="D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt";
//        FileReaderTest test = new FileReaderTest();
////        test.write(fileName,"\r\n数据");
//        log.info("读取文件。。。。");
//        test.read(fileName);

        System.out.println(Integer.MAX_VALUE);

    }

    public void read(String source){
        //方法1.一个一个字符读取：
        try (FileReader fr = new FileReader(source);) {
            int c;
            while ((c = fr.read()) != -1) {
                System.out.print((char) c);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //方法2。用数组读取：
//        try (FileReader fr = new FileReader("D:/code/java/JAVA22/function2framework/src/main/java/com/liwenfeng/test.txt");) {
//            char[] buffer = new char[3];
//            int len;
//            while ((len = fr.read(buffer)) != -1) {
//                System.out.print(new String(buffer,0,len));
//                Thread.sleep(1000);
//            }
//        } catch (IOException | InterruptedException e) {
//            throw new RuntimeException(e);
//        }
    }

    public void write(String target,String content){
        //字符流写入
        try(FileWriter fw = new FileWriter(target,true)){
            fw.write(content);
            fw.flush();
            read(target);
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
